/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.bean.UserRoleRelate;
import com.wubo.sec.service.IUserRoleService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-8
 * @version 1.0.01
 */
@Controller
public class UserRoleMgtAction extends BaseSecAction {
	@Autowired
	private IUserRoleService userRoleService;

	private String userCode;
	
	private List<String> roleCodes;
	
	private String roleCode;

	public IUserRoleService getUserRoleService() {
		return userRoleService;
	}

	public void setUserRoleService(IUserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public List<String> getRoleCodes() {
		return roleCodes;
	}

	public void setRoleCodes(List<String> roleCodes) {
		this.roleCodes = roleCodes;
	}

	public String getRoleCode() {
		return roleCode;
	}

	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}
	
	public String show(){
		try{

			roleCodes = userRoleService.getRoleCodes(userCode);
			
			logger.debug("Find user's roles .UserCode:"+userCode+", RoleCodes:"+roleCodes);
			
			jsonSuccess(new JSONSerializer().serialize(roleCodes), true);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String relate(){
		logger.debug("relate roles to user .UserCode:"+userCode+", RoleCodes:"+roleCode);

		try{
			String[] temp = null;
			if(! StringUtils.isEmpty(roleCode)){
				temp = roleCode.split(",");
				roleCodes  = Arrays.asList(temp);
			}
			
			
			userRoleService.relate(new UserRoleRelate(userCode, temp));
			
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
}
